package com.dev.print;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.dev.print.protocol.ReadWriteFrame;
import com.dev.print.protocol.RtcFrameThread;
import com.dev.print.protocol.base.SPConfig;
import com.dev.print.protocol.callback.ReadFrameCallback;

public class MainActivity extends AppCompatActivity implements ReadFrameCallback, View.OnClickListener {

    private static final String DEVSPPATH = "/dev/ttyS2";
    private static final int SPEED = 115200;
    private static final int FLAGES = 0;

    private RtcFrameThread mRtcFrameThread;
    Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(this);
        start();
    }


    private void start() {
        mRtcFrameThread = new RtcFrameThread(this, new ReadWriteFrame());
        if (mRtcFrameThread.register(new SPConfig(DEVSPPATH, SPEED, FLAGES)))
            mRtcFrameThread.start();
        else
            Toast.makeText(this, "打开串口失败...请检查串口设备节点...", Toast.LENGTH_SHORT).show();
    }
    byte[] newbuffer;
    byte[] line = {(byte)0x0A};//换行
    byte[] small ={(byte)0x1B,(byte)0x56,(byte)0x01};
    byte[] normal ={(byte)0x1B,(byte)0x56,(byte)0x02};
    byte[] big ={(byte)0x1B,(byte)0x56,(byte)0x03};
    byte[] hexKnife = { (byte)0x1B, (byte)0x69 };
    byte[] demo ={(byte)0x1B,(byte)0x66};
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt:
               for(int i = 0;i<3;i++){
                   newbuffer ="123456789!@#$%^&*()".getBytes();

                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(small);
                   mRtcFrameThread.write(newbuffer);

                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(normal);
                   mRtcFrameThread.write(newbuffer);

                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(line);
                   mRtcFrameThread.write(big);
                   mRtcFrameThread.write(newbuffer);
               }
                // 切刀,收到该命令后,切刀执行一次切刀操作;
               // mRtcFrameThread.write(hexKnife);
                break;
        }
    }

    @Override
    public void rtcFrameCallback(byte[] buffer) {

    }

    @Override
    public void completionFrameCallback(byte[] buffer) {

    }

    @Override
    public void dataSetReady() {

    }


}
